<?php
namespace App\Services\Enterprise;
use App\Model\Position;
use App\Services\CommonService;
class PositionService extends CommonService{
    /**
     * 企业中心   职位列表
     * @param $id
     */
    public function PositionList($id){
        $arr=Position::all()->where('company_id',$id)->Take(10)->toArray();
        return $this->success($arr);
    }
    /**
     * 企业中心  删除职位
     */
    public function PositionDel($id){
        $result=Position::where('position_id',$id)->delete();
        if($result){
            return $this->success('删除成功');
        }else{
            return $this->fail('4004');
        }
        return $result;
    }
    /**
     * 企业中心 修改职位的信息
     */
    public function PositionOne($id){
        $arr=Position::where('position_id',$id)->get()->toArray();
        return $this->success($arr);
    }
    /**
     * 企业中心 职位状态
     */
    public function PositionStatus($id,$status){
        $result=Position::where('Position_id',$id)->update(['position_status'=>$status]);
        if($result==1){
            //修改成功
            return $this->success();
        }else{
            //失败
            return $this->fail('4002');
        }
    }
    /**
     * 企业中心  修改职位
     */
    public function PositionUpd($id,$data){
        $data['position_education']=$data['position_education']+1;
        $data['position_workstatu']=$data['position_workstatu']+1;
        $arr=explode('-',$data['position_price']);
        $data['position_moneymin']=$arr[0];
        $data['position_moneymax']=$arr[1];
        unset($data['position_price']);
        $result=Position::where('Position_id',$id)->update($data);
        if($result==1){
            //修改成功
            return $this->success();
        }else{
            //失败
            return $this->fail('4001');
        }
    }
    /**
     * 企业中心 添加职位
     */
    public function PositionAdd($request){
        //解析
        $arr=explode('-',$request->position_price);
        //return $request;
        //更新修改时间
        $update_time = date("Y-m-d h:m:s",time());
        $result = Position::create([
            'position_name'=>$request->position_name,
            'position_workstatu'=>$request->position_workstatu+1,
            'position_address'=>$request->position_address,
            'position_moneymin'=>$arr[0],
            'position_moneymax'=>$arr[1],
            'position_education'=>$request->position_education+1,
            'position_worklife'=>$request->position_worklife,
            'position_type'=>$request->position_type,
            'position_number'=>$request->position_number,
            'industry_id'=>1,
            'company_id'=>1,
            'position_status'=>1,
            'update_time'=>$update_time
        ]);
        //return $result;
        if($result){
            //修改成功
            return $this->success();
        }else{
            //失败
            return $this->fail('4004');
        }
    }
    /**
     * 职位列表
     */
    public function PositionShow(){
        //查询所有的职位列表
        return Position::join('re_enterprise as e','e.id','=','re_position.position_id')->get(['position_id','enterprise_corporate_name','position_name','position_address','position_education','position_moneymax','position_moneymin','enterprise_logo','position_worklife']);
    }
}